<?php


namespace app\controller\admin;


use app\BaseController;
use app\models\config\ConfigModel;
use app\services\config\ConfigService;
use think\facade\Cache;
use think\facade\Db;

class ConfigController extends BaseController
{
    /**
     * 网站配置
     * @return \think\response\Json
     */
    public function setConfig()
    {
        try {
            $params = $this->request->param();
            foreach ($params as $k => $v) {
                if(($k == 'points_pay_proportion' || $k == 'point_redemption_ratio') && ($v < 0 || $v > 100)){
                    throw new \Exception('积分+余额付款填写比例不正确');
                }
                ConfigService::setConfigValue($k, $v);
            }
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 获取配置信息
     * @return \think\response\Json
     */
    public function getConfigInfo()
    {
        try {
            $type = $this->request->param('type', 0);
            $list = ConfigService::getTypeConfigValueArray($type);
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }
}